<?php
include_once 'Tool.php';

class BasicOidGenerator {
	private static $serverId;
	private static $n = 0;
	
	public static function genOid() {
		if(is_null(self::$serverId)){
			self::genServerId();
		}
		
		if(self::$n >= 999){
			self::$n = 0;
		} else {
			self::$n ++ ;
		}
		
		//TODO: convert the $n to a string with 3 digits.
		//if $n is already 3 digits, such as 222, convert it to string
		//if $n is 2 digits, such as 22, convert it to "022"
		if(self::$n < 10)
		{
			self::$n = "00".self::$n;
		}
		//if $n is 1 digits, such as 2, convert it to "002"
		else if (self::$n < 100)
		{
			self::$n = "0".self::$n;
		}
		$sn = ''.self::$n;
		$oid = self::$serverId.Tool::getCurrentPHPTimeStamp().$sn;

		//TODO: convert $oid to an integer
		return (int)$oid;
	}
	
	private static function genServerId() {
		$serverip = $_SERVER['SERVER_ADDR'];
		if(is_null($serverip)){
			self::$serverId = 0;
			return;
		}
		
		$ipdigits = explode('.', $serverip);
		self::$serverId = end($ipdigits);		
	}
}